#include "Admin.h"
#include "StdFile.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define true 1
#define false 0



int LogOn(char *Playername)
{
    printf("请输入用户名:\n");
    char floderpath[20]={0};
    scanf("%s",floderpath);
    printf("请输入密码:\n");
    char password[20]={0};
    scanf("%s",password);
    char temp[100]="./GamePlayer/";
    strcat(temp,floderpath);
    strcat(temp,"_");
    strcat(temp,password);
    strcpy(Playername,temp);
    int a=IsFloderExist(temp);
    if(a==true)
        return true;
    else
        return false;
}
int Register(Player *player,DynamicArray *monster,DynamicArray *treasure)
{
    printf("请输入用户名:\n");
    char floderpath[20]={0};
    scanf("%s",floderpath);
    printf("请输入密码:\n");
    char password[20]={0};
    scanf("%s",password);
    char temp[40]="./GamePlayer/";
    strcat(temp,floderpath);
    strcat(temp,"_");
    strcat(temp,password);
    int a=CreateFloder(temp);
    char playerdata[40]={0};
    strcat(playerdata,temp);
    strcat(playerdata,"/player.txt");
    CreateFile(playerdata);
    char monsterdata[40]={0};
    strcat(monsterdata,temp);
    strcat(monsterdata,"/monster.txt");
    CreateFile(monsterdata);
    char treasuredata[40]={0};
    strcat(treasuredata,temp);
    strcat(treasuredata,"/treasure.txt");
    CreateFile(treasuredata);
    if(a==true)
    {
        printf("创建成功\n");
        FILE *fd1=fopen(playerdata,"w+");
        FILE *fd2=fopen(monsterdata,"w+");
        FILE *fd3=fopen(treasuredata,"w+");
        fwrite(player,sizeof(Player),1,fd1);
        for(int i=0;i<monster->len;i++)
        {
            Monster *monsters=(Monster*)monster->dp[i];
            fprintf(fd2,"%s %d %d %d %d",monsters->name.string,monsters->HP,monsters->attack,monsters->x,monsters->y);
        }
        for(int i=0;i<treasure->len;i++)
        {
            Treasure *treasures=(Treasure *)treasure->dp[i];
            fprintf(fd3,"%s %d %d %d %d",treasures->name.string,treasures->value,treasures->sign,treasures->x,treasures->y);
        }
        fclose(fd1);
        fclose(fd2);
        fclose(fd3);
        return true;
    }
    else
    {
        printf("账户已存在\n");
        return false;
    } 
}
void LoginInterface(Player *player,DynamicArray *monster,DynamicArray *treasure,char *playername)
{
    while(1)
    {
        printf("1、已有账户,直接登录\n");
        printf("2、没有账户,注册账号\n");
        printf("请输入:");
        int temp=0;
        scanf("%d",&temp);
        if(temp==1)
        {
            int a=LogOn(playername);
            if(a==true)
            {
                printf("登录成功\n");
                break;
            }
            else
            {
                printf("账户或密码错误\n");
            }
        }
        if(temp==2)
        {
            int b=Register(player,monster,treasure);
        }    
    }
}

void GameData(Player *player,DynamicArray *monster,DynamicArray *treasure,char *playername)
{
    LoginInterface(player,monster,treasure,playername);
    char playerdata[40]={0};
    strcat(playerdata,playername);
    strcat(playerdata,"/player.txt");
    char monsterdata[40]={0};
    strcat(monsterdata,playername);
    strcat(monsterdata,"/monster.txt");
    char treasuredata[40]={0};
    strcat(treasuredata,playername);
    strcat(treasuredata,"/treasure.txt");
    FILE *fd1=fopen(playerdata,"r");
    FILE *fd2=fopen(monsterdata,"r");
    FILE *fd3=fopen(treasuredata,"r");
    fread(player,sizeof(Player),1,fd1);
    for(int i=0;i<monster->len;i++)
    {
        Monster *monsters=(Monster*)monster->dp[i];
        fscanf(fd2,"%s %d %d %d %d",monsters->name.string,&monsters->HP,&monsters->attack,&monsters->x,&monsters->y);
    }
    for(int i=0;i<treasure->len;i++)
    {
        Treasure *treasures=(Treasure *)treasure->dp[i];
        fscanf(fd3,"%s %d %d %d %d",treasures->name.string,&treasures->value,&treasures->sign,&treasures->x,&treasures->y);
    }
    fclose(fd1);
    fclose(fd2);
    fclose(fd3);
}